package cn.chiship.framework.upms.biz.system.controller;


import cn.chiship.framework.upms.biz.base.service.CacheService;
import cn.chiship.framework.upms.biz.system.entity.UpmsProof;
import cn.chiship.framework.upms.biz.system.entity.UpmsProofExample;
import cn.chiship.framework.upms.biz.system.pojo.dto.UpmsProofDto;
import cn.chiship.framework.upms.biz.system.service.UpmsProofService;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.framework.base.BaseService;
import cn.chiship.sdk.framework.base.BaseController;
import cn.chiship.sdk.core.annotation.Authorization;

import javax.annotation.Resource;
import javax.validation.Valid;

import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.framework.pojo.vo.PageVo;
import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.framework.common.enums.BusinessTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通行票据控制层
 * 2021/9/27
 *
 * @author lijian
 */
@RestController
@Authorization
@RequestMapping("/proof")
@Api(tags = "通行票据")
public class UpmsProofController extends BaseController<UpmsProof, UpmsProofExample> {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsProofController.class);

    @Resource
    private UpmsProofService upmsProofService;

    @Resource
    private CacheService cacheService;

    @Override
    public BaseService getService() {
        return upmsProofService;
    }

    @SystemOptionAnnotation(describe = "密钥分页查询")
    @ApiOperation(value = "密钥分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序方式", defaultValue = "-gmtModified", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = String.class, paramType = "query")
    })
    @GetMapping(value = "/page")
    public ResponseEntity<BaseResult> page(@RequestParam(required = false, defaultValue = "", value = "projectName") String projectName) {
        UpmsProofExample upmsProofExample = new UpmsProofExample();
        /**
         * 创造条件
         */
        UpmsProofExample.Criteria criteria = upmsProofExample.createCriteria();
        criteria.andIsDeletedEqualTo(BaseConstants.NO);
        if (!StringUtil.isNullOrEmpty(projectName)) {
            criteria.andProjectNameLike(projectName + "%");
        }

        PageVo pageVo = super.page(upmsProofExample);
        cacheService.cacheProofs();
        return super.responseEntity(BaseResult.ok(pageVo));
    }

    @SystemOptionAnnotation(describe = "保存密钥", option = BusinessTypeEnum.SYSTEM_OPTION_SAVE)
    @ApiOperation(value = "保存密钥")
    @PostMapping(value = "save")
    public ResponseEntity<BaseResult> save(@RequestBody @Valid UpmsProofDto upmsProofDto) {
        UpmsProof upmsProof = new UpmsProof();
        BeanUtils.copyProperties(upmsProofDto, upmsProof);
        return super.responseEntity(super.save(upmsProof));
    }

    @SystemOptionAnnotation(describe = "更新密钥", option = BusinessTypeEnum.SYSTEM_OPTION_UPDATE)
    @ApiOperation(value = "更新密钥")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataTypeClass = String.class, paramType = "path"),
    })
    @PostMapping(value = "update/{id}")
    public ResponseEntity<BaseResult> update(@PathVariable("id") String id, @RequestBody @Valid UpmsProofDto upmsProofDto) {
        UpmsProof upmsProof = new UpmsProof();
        BeanUtils.copyProperties(upmsProofDto, upmsProof);
        return super.responseEntity(super.update(id, upmsProof));
    }

    @SystemOptionAnnotation(describe = "删除密钥", option = BusinessTypeEnum.SYSTEM_OPTION_REMOVE)
    @ApiOperation(value = "删除密钥")
    @PostMapping(value = "remove")
    public ResponseEntity<BaseResult> remove(@RequestBody @Valid List<String> ids) {
        UpmsProofExample upmsProofExample = new UpmsProofExample();
        upmsProofExample.createCriteria().andIdIn(ids);
        return super.responseEntity(BaseResult.ok(upmsProofService.deleteByExample(upmsProofExample)));
    }

    @SystemOptionAnnotation(describe = "密钥启用&锁定", option = BusinessTypeEnum.SYSTEM_OPTION_UPDATE)
    @ApiOperation(value = "密钥启用&锁定")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataTypeClass = String.class, paramType = "path"),
    })
    @PostMapping(value = "changeIsState/{id}")
    public ResponseEntity<BaseResult> changeIsState(@PathVariable("id") String id, @RequestBody @Valid Boolean isDisabled) {
        UpmsProof upmsProof = upmsProofService.selectByPrimaryKey(id);
        upmsProof.setIsDisabled(Boolean.TRUE.equals(isDisabled) ? BaseConstants.YES : BaseConstants.NO);
        return super.responseEntity(upmsProofService.updateByPrimaryKeySelective(upmsProof));
    }


}
